package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.entity.*;
import com.zzyl.exception.BusinessException;
import com.zzyl.mapper.*;
import com.zzyl.service.RoomService;
import com.zzyl.vo.BedVo;
import com.zzyl.vo.RoomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements RoomService {
    @Autowired
    private RoomTypeMapper roomTypeMapper;
    @Autowired
    private BedMapper bedMapper;
    @Autowired
    private CheckInConfigMapper checkInConfigMapper;
    @Autowired
    private ElderMapper elderMapper;

    //新增房间
    @Override
    public void saveOrUpdateRoom(Room room) {
        //根据typeName查询到typeId
        LambdaQueryWrapper<RoomType> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RoomType::getName,room.getTypeName());
        RoomType roomType = roomTypeMapper.selectOne(wrapper);
        if (roomType == null){
            throw new BusinessException("房间类型不存在");
        }
        room.setTypeId(roomType.getId());
        if (room.getId() == null){
            getBaseMapper().insert(room);
        }else{
            getBaseMapper().updateById(room);
        }
    }

    //查询指定楼层下的房间
    @Override
    public List<RoomVo> getByFloorId(Long floorId) {
        return getBaseMapper().findByFloorId(floorId);
    }

    //查询房间详情
    @Override
    public RoomVo findByRoomId(Long id) {
        Room room = getBaseMapper().selectById(id);
        RoomVo roomVo = BeanUtil.copyProperties(room, RoomVo.class);

        //根据room_id查询床位信息
        LambdaQueryWrapper<Bed> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Bed::getRoomId,id);
        List<Bed> bedList = bedMapper.selectList(wrapper);

        //总床位
        int totalBeds = bedList.size();
        int occupiedBeds = (int)(bedList.stream().filter(e -> e.getBedStatus() == 1).count());
        if (totalBeds != 0 && occupiedBeds != 0){
            roomVo.setOccupancyRate(occupiedBeds * 1.0 / totalBeds);
        }else{
            roomVo.setOccupancyRate(0.0);
        }
        //搞定床位集合
        List<BedVo> bedVoList = bedList.stream().map(e -> {
            BedVo bedVo = new BedVo();
            bedVo.setBedNumber(e.getBedNumber());
            //根据床位号查询服务等级名称
            LambdaQueryWrapper<CheckInConfig> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(CheckInConfig::getBedNumber, e.getBedNumber());
            CheckInConfig checkInConfig = checkInConfigMapper.selectOne(wrapper1);
            if (checkInConfig != null) {
                bedVo.setLname(checkInConfig.getNursingLevelName());
                bedVo.getCheckInConfigVo().setCheckInStartTime(checkInConfig.getCheckInStartTime());
                bedVo.getCheckInConfigVo().setCheckInEndTime(checkInConfig.getCheckInEndTime());
            }
            //根据老人id查询老人姓名
            LambdaQueryWrapper<Elder> wrapper2 = new LambdaQueryWrapper<>();
            wrapper2.eq(Elder::getId, checkInConfig.getElderId());
            Elder elder = elderMapper.selectOne(wrapper2);
            bedVo.setName(elder.getName());
            return bedVo;
        }).collect(Collectors.toList());
        roomVo.setBedVoList(bedVoList);
        return roomVo;
    }

    //查询房间详情
//    @Override
//    public RoomVo findByRoomId(Long id) {
//        //根据id查询到房间信息
//        Room room = getBaseMapper().selectById(id);
//        RoomVo roomVo = BeanUtil.copyProperties(room, RoomVo.class);
//
//
//        LambdaQueryWrapper<Bed> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(Bed::getRoomId,id);
//        List<Bed> bedList = bedMapper.selectList(wrapper);
//        //总床位数、入住床位数、入住率
//        int totalBeds = bedList.size();
//        int occupiedBeds = (int) (bedList.stream().filter(e -> e.getBedStatus() == 1).count());
//        if (totalBeds != 0 && occupiedBeds != 0){
//            roomVo.setOccupancyRate(occupiedBeds * 1.0 / totalBeds);
//        }else{
//            roomVo.setOccupancyRate(0.0);
//        }
//        roomVo.setTotalBeds(totalBeds);
//        roomVo.setOccupiedBeds(occupiedBeds);
//        //搞定床位集合
//        List<BedVo> list = bedList.stream().map(e -> {
//            //4-1 创建VO
//            BedVo bedVo = new BedVo();
//            bedVo.setBedNumber(e.getBedNumber());
//
//            LambdaQueryWrapper<CheckInConfig> wrapper1 = new LambdaQueryWrapper<>();
//            wrapper1.eq(CheckInConfig::getBedNumber, e.getBedNumber());
//            CheckInConfig checkInConfig = checkInConfigMapper.selectOne(wrapper1);
//            if (checkInConfig != null) {
//                bedVo.setLname(checkInConfig.getNursingLevelName());
//                bedVo.getCheckInConfigVo().setCheckInStartTime(checkInConfig.getCheckInStartTime());
//                bedVo.getCheckInConfigVo().setCheckInEndTime(checkInConfig.getCheckInEndTime());
//            }
//            //根据老人id查询老人名称
//            LambdaQueryWrapper<Elder> wrapper2 = new LambdaQueryWrapper<>();
//            wrapper2.eq(Elder::getBedId, e.getId());
//            Elder elder = elderMapper.selectOne(wrapper2);
//            if (elder != null) {
//                bedVo.setName(elder.getName());
//            }
//            return bedVo;
//        }).collect(Collectors.toList());
//        roomVo.setBedVoList(list);
//        return roomVo;
//    }

}
